// pages/newIndex/newIndex.js
const common = require('../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLogin: false,
        isBind: false,
        shopInfo: null,
        relayInfo: null,
        imgSwiper: [],
        newGoods: [],
        goodsList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false,
        popImg: null,
        homePop: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (wx.getStorageSync('token')) {
            this.getShopInfo()
            this.getRelayInfo()
            this.setData({
                isLogin: true
            })
            if (wx.getStorageSync('storeId') != 1 || wx.getStorageSync('parentId') != 0) {
                this.setData({
                    isBind: true
                })
                wx.showTabBar()
            } else {
                wx.hideTabBar()
            }
        } else {
            wx.hideTabBar()
        }
        if (options.userShare) {
            wx.setStorageSync('userShare', options.userShare)
            if (wx.getStorageSync('token')) {
                let data = {
                    belong_refer: options.userShare
                }
                common.post('User/set_belong_refer', data).then(function(res) {}).catch(function(e) {})
            }
        }
        this.setData({
            nowDate: new Date().getTime()
        })
        this.getPopImg()
        this.getImgSwiper()
        this.getNewGoods()
        this.getGoodsList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.setData({
            goodsList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getGoodsList()
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        let userId = wx.getStorageSync('userId')
        let storeId = wx.getStorageSync('storeId')
        if (userId) {
            return {
                title: this.data.relayInfo.share_txt,
                path: '/pages/index/index?userShare=' + userId,
                imageUrl: this.data.relayInfo.share_images
            }
        }
    },

    getShopInfo: function() {
        const that = this
        let data = {
            activity: '785',
            store_id: wx.getStorageSync('storeId')
        }
        common.post('Store/get_store_info', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                shopInfo: res
            })
            wx.setNavigationBarTitle({
                title: res.store_name
            })
        }).catch(function(e) {})
    },

    getRelayInfo: function() {
        const that = this
        let data = {
            activity: '785',
            store_id: wx.getStorageSync('storeId')
        }
        common.post('Store/share_images', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                relayInfo: res
            })
        }).catch(function(e) {})
    },

    getPopImg: function() {
        const that = this
        let data = {
            activity: '785',
            images_id: '49'
        }
        common.post('Images/get_img', data).then(function(res) {
            that.setData({
                popImg: res[0]
            })
        }).catch(function(e) {})
    },

    getImgSwiper: function() {
        const that = this
        let data = {
            activity: '785',
            store_id: wx.getStorageSync('storeId'),
            is_online: '1',
            page: 0,
            limit: 99
        }
        common.post('Slideshow/slideshow_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                imgSwiper: res
            })
        }).catch(function(e) {})
    },

    getNewGoods: function() {
        const that = this
        let data = {
            activity: '785',
            user_id: wx.getStorageSync('userId'),
            have_new_goods: 0,
            new_goods: '1',
            is_sale: '1',
            page: 0,
            limit: 99
        }
        common.post('Goods/goods_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                newGoods: res
            })
        }).catch(function(e) {})
    },

    getGoodsList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        const that = this
        let goodsList = this.data.goodsList
        let data = {
            activity: '785',
            is_goods: "0",
            limit: 10,
            page: this.data.nowPage
        }
        common.post('Goods/goods_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    goodsList: goodsList,
                    isAll: true
                })
            } else {
                goodsList = goodsList.concat(res)
                that.setData({
                    goodsList: goodsList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    closePop: function() {
        this.setData({
            homePop: false
        })
    },

    goLink: function(e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.link
        })
    },

    goShopInfo: function() {
        wx.navigateTo({
            url: '/pages/shopInfo/shopInfo'
        })
    },

    goShareAward: function() {
        wx.navigateTo({
            url: '/pages/shareAward/list/list'
        })
    },

    goPointAward: function() {
        wx.navigateTo({
            url: '/pages/pointAward/list/list'
        })
    },

    goShare: function() {
        wx.navigateTo({
            url: '/pages/share/share'
        })
    },

    goGoodsDetail: function(e) {
        wx.navigateTo({
            url: '/pages/goods/goodsDetail/goodsDetail?goodsId=' + e.currentTarget.dataset.id
        })
    }
})